package com.goldstone.actions;

import java.io.File;
import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.dbb.util.FileUtil;
import com.dbb.util.ImageSizeType;
import com.goldstone.entity.Attachment;
import com.goldstone.service.AttachmentService;
import com.goldstone.service.TagService;

@Namespace("/atta")
@Results( { @Result(name = CrudActionSupport.RELOAD, location = "atta.do", type = "redirect") })
public class AttaAction extends CrudActionSupport<Attachment> {
	
	private static final long serialVersionUID = -6456145401441423430L;
	
	@Autowired
	AttachmentService service;
	@Autowired
	TagService tagService;
	
	private String id;
	private String str;
	private String dialogType;
	private Attachment attachment;

	private List<File> file;   
	private List<String> fileFileName;   
	private List<String> fileContentType;
	private String name;   
	
	public List<String> getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(List<String> fileFileName) {
		this.fileFileName = fileFileName;
	}
	public List<String> getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(List<String> fileContentType) {
		this.fileContentType = fileContentType;
	}
	public void setFile(List<File> file) {
		this.file = file;
	}
	
	public void getStr() {
		attachment = service.get(id);
		if(attachment != null) {
			if("comment".equals(dialogType)) {
				Struts2Utils.renderText(attachment.getComment());
			} else if("tags".equals(dialogType)) {
				Struts2Utils.renderText(attachment.getTagString());
			}
		}
	}
	
	public void addStr() {
		attachment = service.get(id);
		if(attachment != null) {
			if("comment".equals(dialogType)) {
				attachment.setComment(str);
				service.save(attachment);
				Struts2Utils.renderText("success");
			} else if("tags".equals(dialogType)) {
				attachment.setTags(tagService.saveTagString(str));
				service.save(attachment);
				Struts2Utils.renderText("success");
			}
		}
	}
	
	public void saveName() {
		if(id != null) {
			attachment = service.get(id);
			attachment.setName(name);
			service.save(attachment);
		}
	}
	
	@Override
	public String list() throws Exception {
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		filters.add(new PropertyFilter("EQB_deleted", "false"));
		service.find(getPage(), filters);
		return "list";
	}
	@Override
	public String input() throws Exception {
		logger.warn("input");
		return INPUT;
	}

	public void down() throws Exception {
		attachment = service.get(id);
		Struts2Utils.render(attachment.getContentType(), new File(FileUtil.downLoad(attachment)), "Content-disposition:inline;filename=" + new String((attachment.getName() + attachment.getExt()).getBytes("GBK"),"ISO8859_1"));
	}
	
	public void img() throws Exception {
		attachment = service.get(id);
		Struts2Utils.render(attachment.getContentType(), new File(FileUtil.getFilePath(attachment)), "Content-disposition:inline;filename=" + new String((attachment.getName() + attachment.getExt()).getBytes("GBK"),"ISO8859_1"));
	}

	public void imgS() throws Exception {
		attachment = service.get(id);
		Struts2Utils.render(attachment.getContentType(), new File(FileUtil.getFilePath(attachment, ImageSizeType.S)), "Content-disposition:inline;filename=" + new String((attachment.getName() + attachment.getExt()).getBytes("GBK"),"ISO8859_1"));
	}

	public void imgM() throws Exception {
		attachment = service.get(id);
		Struts2Utils.render(attachment.getContentType(), new File(FileUtil.getFilePath(attachment, ImageSizeType.M)), "Content-disposition:inline;filename=" + new String((attachment.getName() + attachment.getExt()).getBytes("GBK"),"ISO8859_1"));
	}
	
	public void upload() throws Exception {
		if(file != null)
			for(int i = 0; i < file.size(); i++) {
				attachment = new Attachment(file.get(i), fileFileName.get(i), fileContentType.get(i));
				service.save(attachment);
				Struts2Utils.renderJson(attachment, "text/plain");
			}
		//return CrudActionSupport.RELOAD;
	}
	
	@Override
	public String delete() throws Exception {
		service.delete(id);
		return RELOAD;
	}
	@Override
	protected void prepareModel() throws Exception {
		if(attachment != null && attachment.getId() != null) {
			attachment = service.get(attachment.getId());
		} else {
			attachment = new Attachment();
		}
	}
	public Attachment getAttachment() {
		return attachment;
	}
	public void setAttachment(Attachment attachment) {
		this.attachment = attachment;
	}
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	public void setStr(String str) {
		this.str = str;
	}
	public String getFileName() {
		return name;
	}
	public void setFileName(String fileName) {
		this.name = fileName;
	}
	public String getDialogType() {
		return dialogType;
	}
	public void setDialogType(String dialogType) {
		this.dialogType = dialogType;
	}
	
}
